package com.yuwubao.uav.tool.controller;

import com.yuwubao.framework.bean.JsonResult;
import com.yuwubao.framework.bean.PageData;
import com.yuwubao.framework.loginFilter.LoginRequired;
import com.yuwubao.uav.tool.service.OrganizationService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Min;

/**
 * 智库联盟机构管理
 */
@RestController
@RequestMapping("/bk")
public class OrganizationController {

    private static final Logger logger = LogManager.getLogger(OrganizationController.class);
    
    @Resource
    private OrganizationService organizationService;

    @LoginRequired
    @PostMapping("/addOrganization")
    @ApiOperation(value = "新增智库机构")
    public JsonResult addOrganization(
            @ApiParam(value = "智库机构名称", required = true) @RequestParam("name") String name,
            @ApiParam(value = "联盟ID") @RequestParam(value = "allianceId",required = false) String allianceId,
            @ApiParam(value = "是否是联盟成员0：不是； 1：是") @RequestParam(value = "isAlliancer",required = false) String isAlliancer,
            @ApiParam(value = "所属区域") @RequestParam(value = "area",required = false) String area,
            @ApiParam(value = "关于机构") @RequestParam(value = "about",required = false) String about,
            @ApiParam(value = "联系电话") @RequestParam(value = "telephone",required = false) String telephone,
            @ApiParam(value = "联系地址") @RequestParam(value = "address",required = false) String address,
            @ApiParam(value = "排序字母，如A,B,C....") @RequestParam(value = "nameChar",required = false) String nameChar,
            @ApiParam(value = "网址") @RequestParam(value = "url",required = false) String url,
            @ApiParam(value = "传真") @RequestParam(value = "fax",required = false) String fax
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("name", name);
            pageData.put("allianceId", allianceId);
            pageData.put("isAlliancer", isAlliancer);
            pageData.put("area", area);
            pageData.put("about", about);
            pageData.put("telephone", telephone);
            pageData.put("address", address);
            pageData.put("url", url);
            pageData.put("fax", fax);
            pageData.put("nameChar", nameChar);

            organizationService.addOrganization(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/updateOrganization")
    @ApiOperation(value = "更新智库机构")
    public JsonResult updateOrganization(
            @ApiParam(value = "智库机构ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "智库机构名称", required = true) @RequestParam("name") String name,
            @ApiParam(value = "联盟ID") @RequestParam(value = "allianceId",required = false) String allianceId,
            @ApiParam(value = "是否是联盟成员0：不是； 1：是") @RequestParam(value = "isAlliancer",required = false) String isAlliancer,
            @ApiParam(value = "所属区域") @RequestParam(value = "area",required = false) String area,
            @ApiParam(value = "关于机构") @RequestParam(value = "about",required = false) String about,
            @ApiParam(value = "联系电话") @RequestParam(value = "telephone",required = false) String telephone,
            @ApiParam(value = "联系地址") @RequestParam(value = "address",required = false) String address,
            @ApiParam(value = "网址") @RequestParam(value = "url",required = false) String url,
            @ApiParam(value = "排序字母，如A,B,C....") @RequestParam(value = "nameChar",required = false) String nameChar,
            @ApiParam(value = "传真") @RequestParam(value = "fax",required = false) String fax
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("name", name);
            pageData.put("allianceId", allianceId);
            pageData.put("isAlliancer", isAlliancer);
            pageData.put("area", area);
            pageData.put("about", about);
            pageData.put("telephone", telephone);
            pageData.put("address", address);
            pageData.put("url", url);
            pageData.put("fax", fax);
            pageData.put("nameChar", nameChar);
            organizationService.updateOrganization(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/deleteOrganization")
    @ApiOperation(value = "删除智库机构")
    public JsonResult deleteOrganization(
            @ApiParam(value = "智库机构ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);

            organizationService.deleteOrganization(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/disableOrEnableOrganization")
    @ApiOperation(value = "禁用或启用智库机构")
    public JsonResult disableOrEnableOrganization(
            @ApiParam(value = "智库机构ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids,
            @ApiParam(value = "0:禁用  1：启用", required = true) @RequestParam("state") String state
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            pageData.put("state", state);

            organizationService.disableOrEnableOrganization(pageData);
            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listOrganization")
    @ApiOperation(value = "获取智库机构列表")
    public JsonResult listOrganization(
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(organizationService.listOrganization(pageData));
            result.setResultNum(organizationService.listOrganizationCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @GetMapping("/listOrganizationAll")
    @ApiOperation(value = "获取智库机构列表所有")
    public JsonResult listOrganizationAll(
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);

            result.setResult(organizationService.listOrganizationAll(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }


    // 智库机构的组织机构设置

    @LoginRequired
    @PostMapping("/addOrgJg")
    @ApiOperation(value = "新增智库单位机构信息")
    public JsonResult addOrgJg(
            @ApiParam(value = "单位机构名称", required = true) @RequestParam("name") String name,
            @ApiParam(value = "智库机构ID",required = true) @RequestParam(value = "orgId") String orgId,
            @ApiParam(value = "关于机构") @RequestParam(value = "about",required = false) String about
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("name", name);
            pageData.put("about", about);
            pageData.put("orgId", orgId);

            organizationService.addOrgJg(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/updateOrgJg")
    @ApiOperation(value = "更新智库机构")
    public JsonResult updateOrgJg(
            @ApiParam(value = "单位机构ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "单位机构名称", required = true) @RequestParam("name") String name,
            @ApiParam(value = "智库机构ID",required = true) @RequestParam(value = "orgId") String orgId,
            @ApiParam(value = "关于机构") @RequestParam(value = "about",required = false) String about
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("name", name);
            pageData.put("about", about);
            pageData.put("orgId", orgId);
            organizationService.updateOrgJg(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/deleteOrgJg")
    @ApiOperation(value = "删除单位机构")
    public JsonResult deleteOrgJg(
            @ApiParam(value = "单位机构ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);

            organizationService.deleteOrgJg(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/disableOrEnableOrgJg")
    @ApiOperation(value = "禁用或启用智库机构单位信息")
    public JsonResult disableOrEnableOrgJg(
            @ApiParam(value = "单位机构ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids,
            @ApiParam(value = "0:禁用  1：启用", required = true) @RequestParam("state") String state
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            pageData.put("state", state);

            organizationService.disableOrEnableOrgJg(pageData);
            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listOrgJg")
    @ApiOperation(value = "获取单位机构列表")
    public JsonResult listOrgJg(
            @ApiParam(value = "智库机构ID") @RequestParam(value = "orgId",required = false) String orgId,
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("orgId", orgId);
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(organizationService.listOrgJg(pageData));
            result.setResultNum(organizationService.listOrgJgCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    // 智库机构的领导设置

    @LoginRequired
    @PostMapping("/addOrgLeader")
    @ApiOperation(value = "新增智库领导信息")
    public JsonResult addOrgLeader(
            @ApiParam(value = "领导名称", required = true) @RequestParam("name") String name,
            @ApiParam(value = "智库机构ID",required = true) @RequestParam(value = "orgId") String orgId,
            @ApiParam(value = "职位") @RequestParam(value = "position",required = false) String position,
            @ApiParam(value = "关于机构") @RequestParam(value = "about",required = false) String about
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("name", name);
            pageData.put("about", about);
            pageData.put("orgId", orgId);
            pageData.put("position", position);

            organizationService.addOrgLeader(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/updateOrgLeader")
    @ApiOperation(value = "更新智库机构")
    public JsonResult updateOrgLeader(
            @ApiParam(value = "领导ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "领导名称", required = true) @RequestParam("name") String name,
            @ApiParam(value = "智库机构ID",required = true) @RequestParam(value = "orgId") String orgId,
            @ApiParam(value = "职位") @RequestParam(value = "position",required = false) String position,
            @ApiParam(value = "关于机构") @RequestParam(value = "about",required = false) String about
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("name", name);
            pageData.put("about", about);
            pageData.put("orgId", orgId);
            pageData.put("position", position);
            organizationService.updateOrgLeader(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/deleteOrgLeader")
    @ApiOperation(value = "删除领导")
    public JsonResult deleteOrgLeader(
            @ApiParam(value = "领导ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);

            organizationService.deleteOrgLeader(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/disableOrEnableOrgLeader")
    @ApiOperation(value = "禁用或启用领导")
    public JsonResult disableOrEnableOrgLeader(
            @ApiParam(value = "领导ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids,
            @ApiParam(value = "0:禁用  1：启用", required = true) @RequestParam("state") String state
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            pageData.put("state", state);

            organizationService.disableOrEnableOrgLeader(pageData);
            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listOrgLeader")
    @ApiOperation(value = "获取领导列表")
    public JsonResult listOrgLeader(
            @ApiParam(value = "智库机构ID") @RequestParam(value = "orgId",required = false) String orgId,
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("orgId", orgId);
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(organizationService.listOrgLeader(pageData));
            result.setResultNum(organizationService.listOrgLeaderCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }



}
